package hn.cch.indicator;

import java.util.Collections;
import java.util.List;

/**
 * VaR:Value at Risk
 */
public class HistoricalVaR {


    /**
     *
     * @param rateOfReturns 收益率Rate Of Return
     * @param confidenceInterval 1-置信区间Confidence Interval
     * @return
     */
    public Double calculate(List<Double> rateOfReturns, Double confidenceInterval){

        //1.收益率按从小到大排序
        Collections.sort(rateOfReturns);

        //
        int k = (int) Math.floor(rateOfReturns.size() * confidenceInterval);
        if (k < 1){
            return null;
        }
        double d = rateOfReturns.size() * confidenceInterval - k;
        double hVAR = (rateOfReturns.get(k) - rateOfReturns.get(k - 1)) * d + rateOfReturns.get(k - 1);
        return hVAR;
    }



}
